package com.wemew.securityapi.config.security;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.security.SecureRandom;


/**
 * 作者 CG
 * 时间 2021/1/13 16:00
 * 注释 密码处理器
 */
@Component
public class DefaultPasswordEncoder implements PasswordEncoder {
    private static final String key="43XY59ZDx8nFPGT7CKaWsW3RKEjLw9aYp8BmTcfLfKxk2Pi5ZLzhoaLjsaKv2ukgmGRBT7w1hzBb4XYRmY5e2tiDtF6d65vnZfzVkHcQHs9F1i1UxCZ2TKWOtXixU4fu";
    private static final SecureRandom random = RandomUtil.getSecureRandom(key.getBytes());
    private static final SecretKey secretKey = KeyUtil.generateKey("AES", 128, random);
    private static DES des = SecureUtil.des(secretKey.getEncoded());

    @Override
    public String encode(CharSequence charSequence) {
        return SecureUtil.md5((charSequence + key).trim());
    }

    @Override
    public boolean matches(CharSequence charSequence, String encodedPassword) {
        return encodedPassword.equals(SecureUtil.md5((charSequence + key).trim()));
    }

    public static void main(String[] args) {
        String md5 = SecureUtil.md5("123456" + key);
        System.out.println(md5);
    }
}
